package pers.liyan.shi.springcloud.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import pers.liyan.shi.springcloud.domain.Dept;
import pers.liyan.shi.springcloud.service.DeptClientService;

import java.util.List;

/**
 * 模拟微服务之间互相调用
 *
 * @author shily
 * @date 2019/5/23 14:42
 */

@RestController
@RequestMapping("/deptConsumer")
public class DeptConsumerController {

    final DeptClientService deptClientService;

    @Autowired
    public DeptConsumerController(DeptClientService deptClientService) {
        Assert.notNull(deptClientService, "deptClientService cannot be null");
        this.deptClientService = deptClientService;
    }

    @GetMapping("/")
    List<Dept> getAll() {
        return deptClientService.getAll();
    }

    @GetMapping("/{deptNo}")
    Dept getByDeptNo(@PathVariable("deptNo") Long deptNo) {
        return deptClientService.getByDeptNo(deptNo);
    }

    @PostMapping("/")
    Dept addDept(@RequestBody Dept dept) {
        return deptClientService.addDept(dept);
    }

    @PutMapping("/")
    Dept updateDept(@RequestBody Dept dept) {
        return deptClientService.updateDept(dept);
    }

    @DeleteMapping("/{deptNo}")
    int deleteByDeptNo(@PathVariable Long deptNo) {
        return deptClientService.deleteByDeptNo(deptNo);

    }

}
